package edu.ar.modulo3.perist.dao.impl.jdbc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ar.modulo3.model.Cliente;
import edu.ar.modulo3.model.Empleado;
import edu.ar.modulo3.model.Reserva;
import edu.ar.modulo3.model.Vuelo;
import edu.ar.modulo3.perist.common.ConnectionManager;
import edu.ar.modulo3.perist.dao.IClienteDAO;
import edu.ar.modulo3.perist.dao.IEmpleadoDAO;
import edu.ar.modulo3.perist.dao.IReservaDAO;
import edu.ar.modulo3.perist.dao.IVueloDAO;
import edu.ar.modulo3.perist.factory.BaseDAOFactory;

public class ReservaDAOJdbcImpl implements IReservaDAO {

	public void save(Reserva enttity) {
		// TODO Auto-generated method stub
		
	}

	public void update(Reserva enttity) {
		// TODO Auto-generated method stub
		
	}

	public void delet(Reserva enttity) {
		// TODO Auto-generated method stub
		
	}

	public List<Reserva> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public Reserva getElemntById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Reserva> getReservaByIdCliente(Long id) {
		
		PreparedStatement st = null;
		ResultSet rs = null;
		Reserva reserva = null;
		List<Reserva> reservalist = new ArrayList<Reserva>();
		
		String sql = "SELECT * FROM Reserva WHERE idCliente = ?";
		
		try {
			
			st = ConnectionManager.getInstance().getConnection().prepareStatement(sql);
			st.setLong(1, id);
			rs = st.executeQuery();			
			
			while( rs.next() ) {
				
				reserva = new Reserva();
				reserva.setIdReserva( rs.getLong("idReserva") );
				reserva.setCliente( getClienteById( rs.getLong("idCliente") ) );				
				reserva.setFechaReserva( rs.getDate("fechaReserva") );
				reserva.setEmpleado( getEmpleadoById( rs.getLong("idEmpleado") ) );
				reserva.setVuelo( getVueloById( rs.getLong("idVuelo") ) );
				reservalist.add( reserva );				
			}
			
					
		} catch (SQLException e) {
			
			System.out.println("ERROR EN EL SATEMENT");
			e.printStackTrace();
		}
		
		return reservalist;
	}
	
	private Cliente getClienteById( Long id ) {
		IClienteDAO clienteDAO = BaseDAOFactory.getInstance().getClienteDAO();
		return clienteDAO.getElemntById(id);
	}
	
	private Empleado getEmpleadoById( Long id ) {
		IEmpleadoDAO empleadoDAO = BaseDAOFactory.getInstance().getEmpleadoDAO();
		return empleadoDAO.getElemntById(id);
	}
	
	private Vuelo getVueloById( Long id ) {
		IVueloDAO vueloDAO = BaseDAOFactory.getInstance().getVueloDAO();
		return vueloDAO.getElemntById(id);
	}

}
